package com.ay.weixin.autoreply.event;

import java.io.IOException;
import java.util.Set;

import javax.servlet.ServletException;

import org.lizar.config.keys.Keys;
import org.lizar.controller.Event;
import org.lizar.controller.loader.EventLoader;
import org.lizar.ioc.Container;
import org.lizar.log.Log;
import org.lizar.log.Logger;
import org.lizar.util.MyMath;
import org.lizar.util.StringHelper;

import com.ay.weixin.autoreply.event.handle.HandleInput;
import com.ay.weixin.config.ProjectConfig;
import com.ay.weixin.fdc.admin.customer.db.IbatisWeiXinProjectDao;
import com.ay.weixin.fdc.admin.customer.domain.WeiXinProject;
import com.ay.weixin.fdc.admin.customer.domain.WeiXinProject.WeiXinProjectStatus;

public class SignatureTest extends Event {

	Log log =Logger.newInstance(this.getClass());
	
	private HandleInput input;
	
	private IbatisWeiXinProjectDao projectDao;
	
	
	@Override
	public void init_property() throws Exception {
		
		input=Container.get(HandleInput.class);
		
	}

	@Override
	public String default_handling_path() {
		return "/sig/*";
	}
	
	

	@Override
	public void handle(EventLoader el) throws ServletException, IOException {
		long wxproject_id=el.queryPathInt(1);
		WeiXinProject project=null;
		try {
			project=projectDao.getById(wxproject_id);
		}catch(Exception e1) {
			el.text("project check failure");
			return;
		}
		if(project==null){
			el.text("project check failure");
			return;
		}
		String echogetStr=el.getStr("echostr");
		boolean sig_result=false;
		try {
			sig_result=checkSignature(el,wxproject_id);
		} catch (Exception e) {
			el.text(e.getMessage());
			return;
		}
		if(!sig_result){
        	el.text("signature check failure");
        	return;
        }
		if(StringHelper.equals(project.getStatus(), WeiXinProjectStatus.TEST.name())){
			el.text(echogetStr);
			return;
		}
		
		if(StringHelper.equals(project.getStatus(), WeiXinProjectStatus.START.name())){
			input.handle_input(el,echogetStr,wxproject_id);
			return;
		}
		
		el.text("project status check failure");
		log.warn("project status check failure:"+wxproject_id);

	}
	

	
	private boolean checkSignature(EventLoader el,long wxproject_id) throws Exception
	{
        // you must define TOKEN by yourself
        if (StringHelper.isNull(ProjectConfig.getStr(wxproject_id,"sys.token"))){
        	throw new Exception("ExceptionTOKEN is not defined!");
        }
       
        String signature=el.getStr("signature");
        String timestamp=el.getStr("timestamp");
        String nonce=el.getStr("nonce");
        String token=ProjectConfig.getStr(wxproject_id,"sys.token");
        Set<String> sort_list=new java.util.TreeSet<String>();
        sort_list.add(token);
        sort_list.add(timestamp);
        sort_list.add(nonce);
        
        String encodegetStr=StringHelper.implode(sort_list);
        encodegetStr=MyMath.SHA1(encodegetStr);
        
        if(StringHelper.equals(signature, encodegetStr)){
        	return true;
        }
        
        
        return false;
	}
	
	
}
